package com.test.mytest.swt;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class TxtFileReadApp {

	protected Shell shell;
	private Text txt_filepath;
	private Label label_1;
	private Text txt_directory;
	private Label label_2;
	private Label label_3;
	private Text txt_size;
	private Text txt_编码;
	private Button button_2;
	private Composite composite_1;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TxtFileReadApp window = new TxtFileReadApp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(710, 434);
		shell.setText("SWT Application");
		shell.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("待分割文件");
		
		txt_filepath = new Text(composite, SWT.BORDER);
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text_1.widthHint = 140;
		txt_filepath.setLayoutData(gd_text_1);
		
		Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog d=new FileDialog(shell);
				String open = d.open();
				if(open==null){
					return;
				}
				txt_filepath.setText(open);
			}
		});
		button.setText("...");
		
		label_1 = new Label(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText(" 输出文件夹");
		
		txt_directory = new Text(composite, SWT.BORDER);
		txt_directory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		button_2 = new Button(composite, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog d=new DirectoryDialog(shell);
				d.setFilterPath("D:\\");
				String text = txt_filepath.getText();
				if(text!=null&&!text.isEmpty()){
					d.setFilterPath(new File(text).getParent());
				}
				String open = d.open();
				if(open==null||open.isEmpty()){
					return;
				}
				txt_directory.setText(open);
			}
		});
		button_2.setText("...");
		
		label_2 = new Label(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("分割文件大小");
		
		txt_size = new Text(composite, SWT.BORDER);
		txt_size.setText("20");
		txt_size.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(composite, SWT.NONE);
		
		label_3 = new Label(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("文件编码格式");
		
		txt_编码 = new Text(composite, SWT.BORDER);
		txt_编码.setText("GBK");
		txt_编码.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		
		composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Button button_1 = new Button(composite_1, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				split(txt_filepath.getText(),txt_directory.getText(),Integer.valueOf(txt_size.getText()),txt_编码.getText());
			}
		});
		button_1.setText("分割");
		new Label(composite, SWT.NONE);

	}

	protected void split(String file, String directory, int size, String charsetName) {
		File f = new File(file);
		try(FileInputStream fis=new FileInputStream(f)) {
			
			System.out.println();
			int readSize=0;
			int max=size*1024*1024;
			byte[] bs=new byte[1024];
			int available = fis.available();
			int totalFileSize=available/max+(available%max>0?1:0);
			
			for (int i = 0; i < totalFileSize; i++) {
				File file2 = new File(directory+"\\"+i+f.getName());
				System.out.println(file2);
				if(!file2.exists()){
					file2.createNewFile();
				}
				int readnow=0;
				FileOutputStream fos=new FileOutputStream(file2);
				while (readnow<max&&readSize<available) {
					int read = fis.read(bs);
					readSize+=read;
					readnow+=read;
					fos.write(bs);
					fos.flush();
				}
				
				fos.close();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
